﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel.Design;

namespace DataAccessor.Tools
{
    public delegate void ConfigurationSettingChangedEventHandler(UCConfiguration sender);
    public partial class UCConfiguration : UserControl
    {
        #region events
        public event ConfigurationSettingChangedEventHandler ConfigurationSettingChanged;
        private void OnConfigurationSettingChanged(UCConfiguration sender)
        {
            if (ConfigurationSettingChanged != null)
            {
                ConfigurationSettingChanged(sender);
            }
        }
        #endregion

        #region properties
        private bool _miniMode = false;
        public bool MiniMode
        {
            get { return _miniMode; }
            set
            {
                _miniMode = value;
                if (_miniMode)
                {
                    this.labelTitle.Text = string.Format("Configuration:{0}", this.txtName.Text);
                    this.picExpend.Image = global::DataAccessor.Tools.Properties.Resources._112_DownArrowLong_Blue_16x16_72;
                    this.panelEditor.Hide();
                    this.Size = new Size(this.Width, 30);
                }
                else
                {
                    this.labelTitle.Text = "Configuration";
                    this.picExpend.Image = global::DataAccessor.Tools.Properties.Resources._112_UpArrowLong_Grey_16x16_72;
                    this.Size = new Size(this.Width, 125);
                    this.panelEditor.Show();
                }
            }
        }

        private HolisticDatabaseConfig _config;
        public HolisticDatabaseConfig Config
        {
            get
            {
                if (_config == null)
                {
                    _config = new HolisticDatabaseConfig();
                }
                _config.Name = this.txtName.Text;
                _config.FilePath = this.txtConfigFile.Text; //string.Format("{0}/{1}.xml", Database.Name, _config.Name);
                return _config;
            }
            set
            {
                _config = value;
                if (_config != null)
                {
                    this.txtName.Text = _config.Name;
                    this.txtConfigFile.Text = _config.FilePath;
                }
            }
        }

        private HolisticDatabase _database;
        public HolisticDatabase Database
        {
            get
            {
                return _database;
            }
            set
            {
                _database = value;
            }
        }

        public override ContextMenuStrip ContextMenuStrip
        {
            get
            {
                return base.ContextMenuStrip;
            }
            set
            {
                this.panelTitle.ContextMenuStrip = value;
                // base.ContextMenuStrip = value;
            }
        }
        #endregion

        public UCConfiguration()
        {
            InitializeComponent();
        }

        private void picExpend_Click(object sender, EventArgs e)
        {
            MiniMode = !MiniMode;
        }

        protected override void OnGotFocus(EventArgs e)
        {
            base.OnGotFocus(e);
            this.panelTitle.BackgroundImage = global::DataAccessor.Tools.Properties.Resources.bgtitle_blue;
        }
        protected override void OnLostFocus(EventArgs e)
        {
            base.OnGotFocus(e);
            this.panelTitle.BackgroundImage = global::DataAccessor.Tools.Properties.Resources.bgtitle_gray;
        }

        private void txtName_TextChanged(object sender, EventArgs e)
        {
            txtConfigFile.Text = string.Format("{0}/{1}.xml", Database.Name, txtName.Text);
            OnConfigurationSettingChanged(this);
        }
    }
}
